package com.atguigu.springcloud.Controller;

import com.atguigu.springcloud.Dao.PaymentDao;
import com.atguigu.springcloud.Pojo.Payment;
import com.atguigu.springcloud.ResponseBody.Body;
import com.atguigu.springcloud.Service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Autowired
    public DiscoveryClient discoveryClient;


//    添加数据
    @RequestMapping(value = "addPayment",method = RequestMethod.POST)
    public Body addPayment(@RequestBody Payment payment){
        Body body = paymentService.addPayment(payment);
        return body;
    }

//    查询数据
    @RequestMapping(value = "queryPayment",method = RequestMethod.GET)
    public Body<Payment> queryPayment(@RequestParam("id") long id){
        Body body = paymentService.queryPayment(id);
        return body;
    }

    @RequestMapping(value = "payment/discovery",method = RequestMethod.GET)
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("*****element:"+service);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info(instance.getInstanceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.discoveryClient;
    }
}
